<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc f">

  <xsl:import href="../../../../base.xsl"/>
  
	<xsl:param name="image_scale" select="'scale-down-to-fit'"/>
  
  <xsl:template match="doc:image">
    <xsl:variable name="src">
    	<xsl:variable name="r" select="if (@high-res) then @high-res else @ref"/>
      <xsl:value-of select="f:resolve-object-id($r, .)" />
      <xsl:if test="not(matches($r,'\.[^/]+$'))">.png</xsl:if>
    </xsl:variable>
  	<xsl:variable name="margin" as="item()*">
  		<xsl:choose>
  			<xsl:when test="$image_scale = 'scale-to-fit'">
  				<xsl:sequence select="(6, 6)"/>
  			</xsl:when>
  			<xsl:when test="@align = 'center'">
  				<xsl:sequence select="(0, 0)"/>
  			</xsl:when>
  			<xsl:when test="@align = 'right'">
  				<xsl:sequence select="(6, 6)"/>
  			</xsl:when>
  			<xsl:otherwise>
  				<xsl:sequence select="(6, 0)"/>
  			</xsl:otherwise>
  		</xsl:choose>
  	</xsl:variable>
    <block margin-top="5mm" margin-bottom="0mm" margin-left="{$margin[1]}mm" margin-right="{$margin[2]}mm">
    	<xsl:apply-templates select="@align"/>
      <external-graphic src="{$src}" width="100%" content-width="{$image_scale}" scaling="uniform"
        scaling-method="resample-any-method"/>
    </block>
    <xsl:if test="normalize-space(doc:note) != ''">
    	<block font-size="6pt" margin-left="{$margin[1]}mm" margin-right="{$margin[2]}mm">
      	<xsl:apply-templates select="@align"/>
      	<xsl:apply-templates select="doc:note/node()" />
      </block>
    </xsl:if>
  </xsl:template>
	
	<xsl:template match="@align">
		<xsl:attribute name="text-align" select="."/>
	</xsl:template>

</xsl:stylesheet>
